package com.hanma.app;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.hanma.app.Server.ServierService;
import com.hanma.app.beans.Persion;
import com.hanma.app.beans.PersionManager;

import java.util.List;

import uisupport.lxl.com.ui_support.R;


public class MainActivity extends Activity {
    PersionManager manager;
    ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            manager = PersionManager.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            manager = null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aidl);
        initView();
        bindService();
    }

    private void initView() {
        findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    manager.addPersion(new Persion(""+Math.random()));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.tv_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    List<Persion>ps = manager.getPersion();
                    Log.e("LXL",""+ps.toString());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void bindService() {
        Intent intent = new Intent(this, ServierService.class);
        bindService(intent,mConnection,BIND_AUTO_CREATE);
    }
}
